package com.knife.dom;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.*;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * create by yongli on 2021-11-11 14:52
 */

public class Generator {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Test
    public void test2() throws Exception {
        RestTemplate restTemplate = new RestTemplate();
        JSONArray array = new JSONArray();

        String path = "/Users/yongli/Desktop/ge112.xlsx";
        List<QisWorkOrderBean> beanList = new ArrayList<>();
        try {
            File file = new File(path);
            Workbook workbook = WorkbookFactory.create(file);
            Sheet sheet = workbook.getSheetAt(0);
            int col = sheet.getRow(0).getPhysicalNumberOfCells();
            int num = sheet.getLastRowNum();


            // 从第2行开始读取
            for (int i = 1; i < num + 1; i++) {

                // 从第二行开始
                Row row = sheet.getRow(i);


                QisWorkOrderBean bean = new QisWorkOrderBean();
                bean.setDeptNo("0100199795");
                bean.setOrderTypeNo("ZV04");
                bean.setProductGroupNo("87");
                String id = row.getCell(1).getStringCellValue();
                bean.setServiceOrderId(id);

                String code = row.getCell(2).getStringCellValue();
                bean.setServiceAgentNo(code);

//                String no = row.getCell(4).getStringCellValue();
//                bean.setFaultElement1(no);
//                bean.setFaultElement2(no);
//                bean.setFaultElement3(no);

                Date create = null;
                try {
//                    row.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
                    if (row.getCell(5).getCellType() == Cell.CELL_TYPE_STRING) {
                        create = sdf.parse(row.getCell(5).getStringCellValue());
                    } else {
                        create = row.getCell(5).getDateCellValue();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                bean.setDispatchingTime(create);

                try {
                    if (row.getCell(6).getCellType() == Cell.CELL_TYPE_STRING) {
                        create = sdf.parse(row.getCell(6).getStringCellValue());
                    } else {
                        create = row.getCell(6).getDateCellValue();
                    }
                } catch (Exception e) {

                }
                bean.setReceivingTime(create);

                create = null;
                try {
                    if (row.getCell(7).getCellType() == Cell.CELL_TYPE_STRING) {
                        create = sdf.parse(row.getCell(7).getStringCellValue());
                    } else {
                        create = row.getCell(7).getDateCellValue();
                    }
                } catch (Exception e) {
                }
                bean.setReportCompletionTime(create);
                bean.setCallTime(create);


                create = null;
                try {
                    if (row.getCell(8).getCellType() == Cell.CELL_TYPE_STRING) {
                        create = sdf.parse(row.getCell(8).getStringCellValue());
                    } else {
                        create = row.getCell(8).getDateCellValue();
                    }
                } catch (Exception e) {
                }
                bean.setCompletionTime(create);


                create = null;
                try {
                    if (row.getCell(9).getCellType() == Cell.CELL_TYPE_STRING) {
                        create = sdf.parse(row.getCell(9).getStringCellValue());
                    } else {
                        create = row.getCell(9).getDateCellValue();
                    }
                } catch (Exception e) {
                }
                bean.setCreateTime(create);

                String userId = row.getCell(10).getStringCellValue();
                bean.setCreateUserId(userId);

                String cusId = row.getCell(11).getStringCellValue();
                bean.setCustId(cusId);

                row.getCell(12).setCellType(Cell.CELL_TYPE_STRING);
                String deviceNo = row.getCell(12).getStringCellValue();
                bean.setDeviceNo(deviceNo);

                String faultType = row.getCell(14).getStringCellValue();
                bean.setFaultLevelNo(faultType);

                bean.setFaultAttributionNo("1");
                bean.setFailureCause("1");

                String des = row.getCell(16).getStringCellValue();
                bean.setProcessResult(des);

                System.out.println(JSONObject.toJSONString(bean));
                System.out.println(JSONObject.toJSONString(bean.getCompletionTime()));
                beanList.add(bean);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }


        for (int i = 1; i < beanList.size(); i++) {
            QisWorkOrderBean params = beanList.get(1);
            if (params.getCompletionTime() != null) {
                params.setCompletionTime(params.getCallTime());
            }
            Thread.sleep(150);
            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Basic cWlzcHJvZDplRUpZdXQ1Q2pa");
            System.out.println(JSON.toJSONString(params));

            HttpEntity<Object> httpEntity = new HttpEntity<>(params, headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(
                    "https://qis.sany.com.cn/ws/dom/importServiceOrder", httpEntity, String.class);
            System.out.println(responseEntity);
        }
    }
}
